<?php

class clock extends bhCommon {

    public function __construct()
    {
        parent::__construct();
    }



    public function index()
    {
        $today = $this->get('today', 1);
        $same_day = strtotime(date('Y-m-d'));

        $bonus = array();
        if ($today == 2) {
            $same_day = $same_day - 86400;

            $system_day = p_get('system_day', array('same_day' => $same_day));
            $system_day['currency'] = floatval($system_day['currency']);

            $bonus['system_day'] = $system_day;

            $bonus['own'] = p_get('day', array('member_id' => $this->member['id'], 'same_day' => $same_day));
            if ($bonus['own']) {
                $bonus['own']['currency'] = floatval($bonus['own']['currency']);
            }

        }

        $my_num = p_getcolumn('currency_log', array('member_id' => $this->member['id'], 'same_day' => $same_day, 'type' => 1), 'COUNT(*)') ?: 0;
        $one_num = p_getcolumn('currency_log', array('member_id' => $this->member['id'], 'same_day' => $same_day, 'type' => 2), 'COUNT(*)') ?: 0;
        $two_num = p_getcolumn('currency_log', array('member_id' => $this->member['id'], 'same_day' => $same_day, 'type' => 3), 'COUNT(*)') ?:0;

        $config = array(
            'own_currency' => getConfig('own_currency', 1),
            'one_currency' => getConfig('one_currency', 1),
            'two_currency' => getConfig('two_currency', 1),
            'grid_ad' => getad(),
            'currency_name' => getConfig('currency_name'),
            'audit_model' => !audit_model(),
            'ad_type' => getConfig('ad_type', 1),
            'is_bonus' => getConfig('is_bonus', 1),
        );
        $config['ad_show_type'] = $this->rising->get_ad_type($config['ad_type']);
        $data = array(
            'config' => $config,
            'my_num' => $my_num,
            'one_num' => $one_num,
            'two_num' => $two_num,
            'bonus' => $bonus
        );
        $this->json($data);
    }



    public function log()
    {
        $type = $this->get('level', 1);

        $page_size = 10;
        $p = $this->get('p', 1);
        $limit = ($p - 1) * $page_size;

        $today = $this->get('today', 1);
        $same_day = strtotime(date('Y-m-d'));
        if ($today == 2) {
            $same_day = $same_day - 86400;
        }

        $w = array(
            'member_id' => $this->member['id'],
            'same_day' => $same_day,
            'type' => $type,
        );
        $log = p_getall('currency_log', $w, array(),'', array('id asc'), "{$limit}, {$page_size}");

        if ($log) {
            foreach ($log as & $value) {
                if ($value['type'] > 1) {
                    $value['member'] = p_get('member', array('id' => $value['remarks']));
                } else {
                    $value['member'] = $this->member;
                }
                $value['created'] = date('Y-m-d H:i:s', $value['created']);
            }
        }

        $this->json(array('log' => $log));
    }



    public function sign()
    {
        if ($this->member['is_block'] == 2) {
            $this->json(getConfig('block_msg', '打卡失败'), 2);
        }

        if (getConfig('is_captcha') == 2) {
            if (!$this->rising->checkcaptcha($this->member['id'], $this->get('captcha_code'))) {
                $this->json($this->rising->error, 2);
            }
        }

        $bd_img = '';
        if ($baduanjin = getConfig('baduanjin_images')) {
            $baduanjin = json_decode($baduanjin, true);
            $k = array_rand($baduanjin);
            $bd_img = getImage($baduanjin[$k]);
        }
        $same_day = strtotime(date('Y-m-d'));
        if (getConfig('pattern', 1) == 2) {
            $subsection = $this->rising->subsection($this->member['id']);
            if ($subsection['state'] == 1) {
                pdo_begin();
                try{
                    $i = array(
                        'member_id' => $this->member['id'],
                        'same_day' => $same_day,
                        'created' => time(),
                    );
                    if (!p_insert('member_clock', $i)) {
                        throw new Exception('打卡失败');
                    }

                    if (!$this->rising->change_currency($this->member['id'], $subsection['subsection']['currency'], 1)) {
                        throw new Exception($this->rising->error);
                    }

                    if ($this->member['parent_id'] > 0) {
                        $one_currency = getConfig('one_currency', 0);
                        if ($one_currency > 0) {
                            if (!$this->rising->change_currency($this->member['parent_id'], $one_currency,2, $this->member['id'])) {
                                throw new Exception($this->rising->error);
                            }
                        }

                        $two = p_get('group', array('downline_id' => $this->member['id'], 'level' => 2));
                        $two_currency = getConfig('two_currency', 0);
                        if ($two && $two_currency > 0) {
                            if (!$this->rising->change_currency($two['member_id'], $two_currency,3, $this->member['id'])) {
                                throw new Exception($this->rising->error);
                            }
                        }
                    }
                    p_update('member', array('click' => $this->member['click'] + 1), array('id' => $this->member['id']));


                    pdo_commit();
                } catch (Exception $e) {
                    pdo_rollback();
                    $this->json($e->getMessage(), 2);
                }
            }

            $this->json(array('bd_img' => $bd_img));
        }


        if (!$this->rising->is_time_slot()) {
            $this->json('现在不在打卡时间范围内', 2);
        }

        $today = $this->rising->get_clock_today($this->member['id']);
        $clock_num = getConfig('clock_num', 10);
        if ($clock_num <= $today['clock']) {
            $this->json('今日打卡已达到上限,明日再来', 2);
        }


        $clock_interval = getConfig('clock_interval', 0);
        $sql = "SELECT * FROM " . prefix('member_clock') . " WHERE member_id={$this->member['id']} ORDER BY id DESC";

        if ($last = p_fetch($sql)) {
            if (time() < ($last['created'] + $clock_interval + 15)) {

                $abnormal = $this->member['abnormal'] + 1;
                $abnormal_abnormal = getConfig('abnormal_abnormal', 0);
                $u = array(
                    'abnormal' => $abnormal
                );

                if ($abnormal_abnormal > 0 && $abnormal_abnormal <= $abnormal) {
                    $u['is_block'] = 2;
                }

                p_update('member', $u, array('id' => $this->member['id']));
                $this->json('打卡失败', 2);
            }
        }


        pdo_begin();
        try{
            $i = array(
                'member_id' => $this->member['id'],
                'same_day' => $same_day,
                'created' => time(),
            );
            if (!p_insert('member_clock', $i)) {
                throw new Exception('打卡失败');
            }
            $own_currency = getConfig('own_currency');

            if ($this->rising->isOpenDouble() && $this->get('click_ad') == 2) {
                $own_currency = $own_currency * 2;
            }

            if (!$this->rising->change_currency($this->member['id'], $own_currency, 1)) {
                throw new Exception($this->rising->error);
            }
            if ($this->member['parent_id'] > 0) {
                $one_currency = getConfig('one_currency', 0);
                if ($one_currency > 0) {
                    if (!$this->rising->change_currency($this->member['parent_id'], $one_currency,2, $this->member['id'])) {
                        throw new Exception($this->rising->error);
                    }
                }

                $two = p_get('group', array('downline_id' => $this->member['id'], 'level' => 2));
                $two_currency = getConfig('two_currency', 0);
                if ($two && $two_currency > 0) {
                    if (!$this->rising->change_currency($two['member_id'], $two_currency,3, $this->member['id'])) {
                        throw new Exception($this->rising->error);
                    }
                }
            }

            p_update('member', array('click' => $this->member['click'] + 1), array('id' => $this->member['id']));


            pdo_commit();

            if ($this->rising->isSend()) {
                $clock = p_getcolumn('day', array('member_id' => $this->member['id']), 'SUM(clock)') ? : 0;
                if ($clock < 2 && !p_get('money_log', array('member_id' => $this->member['id'], 'type' => 3))) {

                    $t = strtotime(date('Y-m-d'));
                    $sql = 'SELECT COUNT(*) FROM ' . prefix('money_log') . ' WHERE `type`=3 AND created>' . $t . get_uniacid();
                    $count = p_fetchcolumn($sql) ? : 0;
                    if ($count < getConfig('rank_number', 0)) {
                        $money = getConfig('rank_money', 0.3);
                        $this->rising->change_money($this->member['id'], $money, 3);
                    }
                }
            }


            $this->json(array('bd_img' => $bd_img));
        } catch (Exception $e){
            pdo_rollback();
            $this->json($e->getMessage(), 2);
        }
    }



    /**
     *
     */
    public function captcha()
    {
        defined('IN_IA') or exit('Access Denied');
        error_reporting(0);
        load()->classs('captcha');


        $captcha = new Captcha();
        $captcha->build(145, 40);
        $hash = md5(strtolower($captcha->phrase) . $this->w['config']['setting']['authkey']);

        p_delete('code', array('member_id' => $this->member['id']));
        $insert = array(
            'code' => $hash,
            'member_id' => $this->member['id'],
            'created' => time()
        );
        p_insert('code', $insert);

        $captcha->output();
    }


}